package com.scalar.chiptrack.finance.proformainvoice.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.proformainvoice.dao.ProformaInvoiceDAO;
import com.scalar.chiptrack.finance.proformainvoice.forms.ProformaInvoiceForm;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.SalesOrderDAO;
import com.scalar.chiptrack.operations.process.dao.FinishedGoodsDAO;
import com.scalar.chiptrack.operations.process.FinishedGoods;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * User: KP
 * Date: Sep 28, 2006
 * Time: 3:10:16 PM
 */
public class ProformaInvoiceSearchNewCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/newproformaInvoice.do";

    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        /*
            Variable 'screenMode' used to determine where this class is called from Fab Lots for new releases search scr and from pulling new fab lots from modify release scr
            Variable 'searchWithCompulsoryFields' used to show the Fab lots results for generating new releases.
            Variable 'parentWindowActionType' used to in popup scr which shows the Fab lots results, which need to be pulled for new releases or existing releases.
            Variable 'closeWindowAfterSave' used to in popup scr which is used to close the popup scr after saving(pulling).
        */
        String forward                      = "search_orders_for_new_invoice";
        String screenMode                   = request.getParameter("screenMode");
        String action                       = request.getParameter("action");
        String searchWithCompulsoryFields   = request.getParameter("searchWithCompulsoryFields");
        String turnKeySelected              = request.getParameter("turnKeySelected");

        HashMap errors          = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info            = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        HttpSession mSession    = request.getSession();
        User user               = ( User ) mSession.getAttribute ( SessionKeys.USER_KEY );

        ProformaInvoiceForm invoiceForm = ( ProformaInvoiceForm ) form;

        if( errors == null )
        {
            errors = new HashMap();
        }
        if( info == null )
        {
            info = new HashMap();
        }
        if ( user == null )
        {
            if( ACTION_POPUP.equalsIgnoreCase( screenMode ) )
            {
                request.setAttribute( "popupWindowSessionExp", "true" );
                request.setAttribute( "showPOPUP" , "SHOWPOPUP" );
                return ( mapping.findForward ( "search_orders_for_popup_scr" ) );
            }
            else
            {
                mSession.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME);
                return ( mapping.findForward ( "login" ) );
            }
        }
        else
        {
            if ( user.isActionAllowed( Functionality.PRO_FORMA_INVOICE , User.USER_ACTION_MODIFY ) )
            {
                request.setAttribute ( "showMessage", "FALSE" );
            }
            else
            {
                request.setAttribute ( "showMessage", "TRUE" );
            }
        }

        try
        {
            //System.out.println("$$$$$ 1-1--SearchNewCommand--searchWithCompulsoryFields = " + searchWithCompulsoryFields + " SearchSalesOrderId() = " + invoiceForm.getSearchSalesOrderId() );
            if( "true".equalsIgnoreCase( searchWithCompulsoryFields ) && ( invoiceForm.getSearchSalesOrderId() > 0 ) )
            {
                //invoiceForm.getLineItemList();

                if( invoiceForm.getLineItemList() == null || invoiceForm.getLineItemList().size() == 0 )
                {
                    info.put("No Sales Orders Available for the given search criteria", null );
                }

            }
            else
            {
                //ReleaseHelper.resetTestinvoiceForm( invoiceForm );
                invoiceForm.setLineItemList( new ArrayList() );
            }


        }
        catch( Exception e )
        {
            e.printStackTrace();
            errors.put( "Could not search new Proforma Invoices", null );
        }

        request.setAttribute( "salesOrderList", ProformaInvoiceDAO.loadApprovedSalesOrderList( null ) );
        request.setAttribute( "customerList", CustomerDAO.getAllCustomerNamesByUserType( null, user ) );
        request.setAttribute ( "productList", ProformaInvoiceDAO.loadManufacturingProducts() );
        request.setAttribute ( "years", SalesOrderDAO.getYearList () );
        request.setAttribute( "salesOrderCustomerPOList", ProformaInvoiceDAO.loadApprovedSalesOrderCustomerPOList( null ) );

        //saveToken( request );
        return mapping.findForward( forward );
    }

}
